package cn.myfirefly.moming.portal.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.myfirefly.moming.common.model.MenuDto;
import cn.myfirefly.moming.portal.dao.mapper.FunctionMapper;
import cn.myfirefly.moming.portal.entity.MMFunction;
import cn.myfirefly.moming.portal.model.FunctionDto;
import cn.myfirefly.moming.portal.model.FunctionFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.FilterUtil;
import cn.myfirefly.utils.StrUtil;

@Repository
public class FunctionDao extends BaseDao<MMFunction>{
	
	@Autowired
	private FunctionMapper functionMapper;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageResultDto<MMFunction> getList(FunctionFilterDto dto, Integer systemId) {
		Session session = getCurrentSession();
		PageResultDto resultDto = new PageResultDto();
		
		StringBuffer countSql = new StringBuffer("select count(r) from " + MMFunction.class.getCanonicalName() + " r where 1=1 and r.systemId=? ");
		StringBuffer querySql = new StringBuffer("from " + MMFunction.class.getCanonicalName() + " r where 1=1 and r.systemId=? ");
		
		List paramList = new ArrayList();
		paramList.add(systemId);
		FilterUtil.handleFilterSql(dto, "r", countSql, querySql, paramList);
		
		Query query = session.createQuery(countSql.toString());
		FilterUtil.setQueryParams(query, paramList);
		Number number = (Number)query.uniqueResult();
		resultDto.setTotal(number.intValue());
		if (number.intValue() > 0) {
			query = session.createQuery(querySql.toString());
			FilterUtil.setQueryParams(query, paramList);
			List<MMFunction> list = query
					.setFirstResult((dto.getPageCurrent() - 1) * dto.getPageSize())
					.setMaxResults(dto.getPageSize())
					.list();
			resultDto.setPageCurrent(dto.getPageCurrent());
			resultDto.setList(list);
		}
		return resultDto;
	}
	
	@SuppressWarnings("unchecked")
	public MMFunction getByCode(String funcId) {
		Session session = getCurrentSession();
		MMFunction func = null;
		Query query = session.createQuery("from " + MMFunction.class.getCanonicalName() + " r where r.funId=? ");
		query.setString(0, funcId);
		List<MMFunction> list = query.list();
		if (list != null && list.size() > 0) {
			func = list.get(0);
		}
		return func;
	}
	
	@SuppressWarnings("unchecked")
	public List<MMFunction> getChildren(String funcId) {
		Session session = getCurrentSession();
		Query query = session.createQuery("from " + MMFunction.class.getCanonicalName() + " r where r.parentFunId=? ");
		query.setString(0, funcId);
		return query.list();
	}
	
	public List<MenuDto> getMenus(String loginName, Integer systemId) {
		List<FunctionDto> list = functionMapper.getMenus(systemId, loginName);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			dto.setPId(functionDto.getParentFunId());
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
	
	public List<MenuDto> getMenus(Integer systemId) {
		List<FunctionDto> list = functionMapper.getMenusBySystemId(systemId);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			dto.setPId(functionDto.getParentFunId());
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
	
	/**
	 * 取得不包括顶级菜单的所有功能菜单
	 * @return
	 */
	public List<MenuDto> getAllMenusExcludeTop(Integer systemId) {
        List<FunctionDto> list = functionMapper.getAllMenusExcludeTopBySystemId(systemId);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			dto.setPId(functionDto.getParentFunId());
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
	
	public List<MenuDto> getAllMenusExcludeTop(String loginName, Integer systemId) {
		List<FunctionDto> list = functionMapper.getAllMenusExcludeTop(systemId, loginName);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			dto.setPId(functionDto.getParentFunId());
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
	
	/**
	 * 取得所有顶级菜单
	 * @return
	 */
	public List<MenuDto> getAllTopMenus(Integer systemId) {
		List<FunctionDto> list = functionMapper.getAllTopMenus(systemId);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			dto.setPId(functionDto.getParentFunId());
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
	
	/**
	 * 取得所有菜单
	 * @return
	 */
	public List<MenuDto> getAllMenus(String inSysId) {
		List<FunctionDto> list = functionMapper.getAllMenus(inSysId);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			if (StrUtil.isBlank(functionDto.getParentFunId())) {
				dto.setPId(functionDto.getSystemId() != null ? functionDto.getSystemId().toString() : null);
			} else {
				dto.setPId(functionDto.getParentFunId());
			}
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
	
	public List<MenuDto> getAllMenus(Integer sysId) {
		List<FunctionDto> list = functionMapper.getAllMenusBySystemId(sysId);
		List<MenuDto> menuDtos = new ArrayList<MenuDto>();
		MenuDto dto = null;
		for (FunctionDto functionDto : list) {
			dto = new MenuDto();
			dto.setId(functionDto.getFunId());
			dto.setName(functionDto.getFunName());
			if (StrUtil.isBlank(functionDto.getParentFunId())) {
				dto.setPId(functionDto.getSystemId() != null ? functionDto.getSystemId().toString() : null);
			} else {
				dto.setPId(functionDto.getParentFunId());
			}
			dto.setUrl(functionDto.getLinkPage());
			dto.setOpenIcon(functionDto.getOpenIcon());
			dto.setCloseIcon(functionDto.getCloseIcon());
			dto.setFunIndex(functionDto.getFunIndex());
			menuDtos.add(dto);
		}
		return menuDtos;
	}
}
